import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/event_bus_utils.dart';
import 'package:wallet_utils/msg_event_utils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wallet_utils/el_refresh.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/toast.dart';
import 'package:wallet_utils/webview_common.dart';
import '';
import '../../home/api/hxh_api.dart';
import '../../home/bean/hxhAuditResultBean.dart';
import '../../home/bean/hxhConfirmLoanBean.dart';
import '../../home/bean/hxhOrderBean.dart';
import '../bean/loanDocBean.dart';
import 'state.dart';
import 'dart:convert';

class LoanDocLogic extends GetxController {
  final LoanDocState state = LoanDocState();
  List<OrderList>? orderList = [];
  @override
  void onInit()  {
    super.onInit();
    EventBusUtils.getInstance().on<MsgEventBusUtils>().listen((event) {
      //是否显示产品状态
        state.refreshController = RefreshController(initialRefresh: false);
        getUserOrder();
    });
    getUserOrder();
  }

  ///订单列表
  getUserOrder() {
    HXHApi.getUserAllOrder(BaseCallback(onSuccess: (map) {
      // String jsonString = '{"code":200,"msg":"success","data":[{"userId":3106669,"applyId":2288617,"thirdPartId":308,"productId":502,"orderNo":"ane-hxh-1706841751739","auditStatus":120,"auditStatusStr":"审核失败","auditPeriodNum":null,"auditAmount":null,"auditRejectReasons":"审核失败","auditYearRate":null,"orderStatus":null,"orderStatusStr":null,"orderUpdateTimeStr":null,"orderActualAmount":null,"orderContAmount":null,"orderLoanPeriodNum":null,"orderYearRate":null,"orderTerminationType":null}]}';
      // Map<String, dynamic> user = json.decode(jsonString);
      HxhOrderBean orderBean = HxhOrderBean.fromJson(map);
      if (orderBean.data != null) {
        orderList = orderBean.data;
      } else {
        ToastUtils.showToast(orderBean.msg ?? "");
      }
      update();
      refreshCompelet(state.refreshController);
    }, onError: (msg) {
      Future.delayed(const Duration(milliseconds: 500), () {
        ToastUtils.showToast(msg ?? "");
        update();
        refreshCompelet(state.refreshController);
      });
    }));
  }
  //获取还款页面接口
  getRepayUrl(String orderNo) async{
    HXHApi.getRepayUrl(
        orderNo,
        BaseCallback(onSuccess: (map) {
          HxhConfirmLoanBean hxhConfirmLoanBean =
          HxhConfirmLoanBean.fromJson(map);
          state.url = hxhConfirmLoanBean.result.url as String?;
          Get.to(WebViewCommon(
            state.url ?? '',
          ));
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
            update();
            Get.to(WebViewCommon(
              state.url ?? '123123',
            ));
          });
        }));
  }
  //确认借款
  confirmLoan(String orderNo) {
    HXHApi.confirmLoan(
        orderNo,
        BaseCallback(onSuccess: (map) {
          HxhConfirmLoanBean hxhConfirmLoanBean =
          HxhConfirmLoanBean.fromJson(map);
          state.url = hxhConfirmLoanBean.result.url as String?;
          Get.to(WebViewCommon(
            state.url ?? '',
          ))?.then((value) {
            getUserOrder();
          });
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
            update();
          });
        }));
  }
  //功能按钮点击事件
  functionAction(num auditStatus,num orderStatus,String orderNo){
    if(auditStatus==110){
      //审核中
      Get.toNamed(BaseRouterConfig.hxhAudit);
    }else if(auditStatus==120){
      //审核拒绝
      Get.toNamed(BaseRouterConfig.auditRejection);
    }else {
      if (orderStatus == 150) {
        confirmLoan(orderNo);
      } else if (orderStatus == 152) {
        confirmLoan(orderNo);
      } else if (orderStatus == 210) {
        Get.offNamed(BaseRouterConfig.repayment);
      } else {
        return '';
      }
    }
  }
  //cell点击事件
  didClickAction(num auditStatus,num orderStatus,String orderNo){
    if(auditStatus==110){
      //审核中
      Get.toNamed(BaseRouterConfig.hxhAudit , arguments: {
        'enterType': 1,
      });
    }else if(auditStatus==120){
      //审核拒绝

    }else {
      Get.toNamed(BaseRouterConfig.hxhOrderDetails,arguments: {
        'orderNo':orderNo,
      });
    }
  }
//获取角标图片
   getTagImge(num auditStatus,num orderStatus){
    if(auditStatus==110){
      return 'images/re_tag_shenhezhong.png';
    }else if(auditStatus==120){
      return 'images/re_tag_shenhejujue.png';
    }else{
      //150:待签约152:绑卡成功250：签约中190：待放款(签约成功)200：放款失败210：放款成功211：还款正常220：已逾期230：提前结清240：正常结清160：取消订单，比如:超过签约时效，自动取消订单245：紧急划回260：用户放弃
      if(orderStatus==150){
        return 'images/re_tag_daiqianyue.png';
      }else if(orderStatus==152){
        return 'images/re_tag_bangka.png';
      }else if(orderStatus==250){
        return 'images/re_tag_qianyuezhong.png';
      }else if(orderStatus==190){
        return 'images/re_tag_daifangkuan.png';
      }else if(orderStatus==210){
        return 'images/re_tag_yifangkuan.png';
      }else{
        return 'images/re_tag_yifangkuan.png';
      }
    }
  }
  //获取功能按钮文字
  getButtonTitle(num auditStatus,num orderStatus){
    if(auditStatus==110){
      //审核中
      return '';
    }else if(auditStatus==120){
      //审核失败
      return '';
    }else{
      //150:待签约 152:绑卡成功 250：签约中 190：待放款(签约成功) 200：放款失败 210：放款成功 211：还款正常220：已逾期230：提前结清240：正常结清160：取消订单，比如:超过签约时效，自动取消订单245：紧急划回260：用户放弃
      if(orderStatus==150){
        return '去签约';
      }else if(orderStatus==152){
        return '去签约';
      }else if(orderStatus==210){
        return '去还款';
      }else{
        return '';
      }
    }
  }


  //审核结果
  // getAuditResult() {
  //   HXHApi.getAuditResult(
  //       state.orderNo!,
  //       BaseCallback(onSuccess: (map) {
  //         HxhAuditResultBean resultBean = HxhAuditResultBean.fromJson(map);
  //           // Get.back();
  //           state.institutionProductName =
  //               resultBean.data?.institutionProductName;
  //           state.auditTime = resultBean.data?.auditTime;
  //           state.status = resultBean.data?.status;
  //           state.auditPeriodNum = resultBean.data?.auditPeriodNum;
  //           state.auditAmount = resultBean.data?.auditAmount;
  //           update();
  //       }, onError: (msg) {
  //         Future.delayed(const Duration(milliseconds: 500), () {
  //           ToastUtils.showToast(msg ?? "");
  //           update();
  //         });
  //       }));
  // }

}
